package org.example.ws;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.*;
import jakarta.websocket.server.ServerEndpoint;
import org.example.config.GetHttpSessionConfig;
import org.example.utils.MessageUtils;
import org.example.ws.pojo.Message;
import org.example.ws.pojo.MessageTest;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ClassName: TaskNotify
 * Description:
 *
 * @Author Kotaro
 * @Create 2025/1/9 16:03
 * @Version 1.0
 */
@ServerEndpoint(value = "/taskNotify" , configurator = GetHttpSessionConfig.class)
@Component
public class TaskNotify {

    //正在浏览任务的志愿者列表
    private static final Map<String, Session> volunteersViewingTasks = new ConcurrentHashMap<>();

    // 用于存储用户的未读消息数量
    private static final Map<String, Integer> unreadMessages = new ConcurrentHashMap<>();

    private HttpSession httpSession;

    /**
     * 建立 websocket 连接后，被调用
     * @param session
     * @param config
     */
    @OnOpen
    public void onOpen(Session session , EndpointConfig config){
        //1.将session进行保存
        this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
        String volunteerId = (String)this.httpSession.getAttribute("volunteerId");
        volunteersViewingTasks.put(volunteerId , session);
    }

    /**
     * 浏览器发送信息到服务器，该方法被调用
     * @param message
     */
    @OnMessage
    public void onMessage(String message){
        try{

        }catch (Exception e){
            //记录日志
        }
    }

    /**
     * 断开 websocket 连接时被调用
     * @param session
     */
    @OnClose
    public void onClose(Session session){

    }

}
